package com.fuqiang.nightshopadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuqiang.nightshopadmin.mapper.IUmsMenuMapper;
import com.fuqiang.nightshopadmin.model.UmsMenuNode;
import com.fuqiang.nightshopadmin.service.IUmsMenuService;
import com.fuqiang.nightshopumsmodel.entity.UmsMenu;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台菜单表 服务实现类
 * </p>
 *
 * @author fuqiang
 * @since 2020-04-14
 */
@Service
@AllArgsConstructor
public class UmsMenuServiceImpl extends ServiceImpl<IUmsMenuMapper, UmsMenu> implements IUmsMenuService {

    private IUmsMenuMapper umsMenuMapper;
    @Override
    public IPage<UmsMenu> list(Long parentId, Integer pageSize, Integer pageNum) {
        QueryWrapper<UmsMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UmsMenu::getParentId,parentId);
        Page<UmsMenu> page = new Page<>(pageNum,pageSize);
        return umsMenuMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Integer update(Long id, UmsMenu umsMenu) {
        // 设置id
        umsMenu.setId(id);
        updateLevel(umsMenu);
        return umsMenuMapper.updateById(umsMenu);
    }

    @Override
    public Integer create(UmsMenu umsMenu) {
        umsMenu.setCreateTime(LocalDateTime.now());
        updateLevel(umsMenu);
        return umsMenuMapper.insert(umsMenu);
    }

    @Override
    public List<UmsMenuNode> treeList() {
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(new QueryWrapper<UmsMenu>());
        List<UmsMenuNode> collect = umsMenus.stream()
                .filter(umsMenu -> umsMenu.getParentId() == 0L)
                .map(umsMenu -> covertMenuNode(umsMenu, umsMenus))
                .collect(Collectors.toList());
        return collect;
    }

    /**
     * 将UmsMenu转化为UmsMenuNode并设置children属性
     */
    private UmsMenuNode covertMenuNode(UmsMenu menu, List<UmsMenu> menuList) {
        UmsMenuNode node = new UmsMenuNode();
        BeanUtils.copyProperties(menu, node);
        List<UmsMenuNode> children = menuList.stream()
                .filter(subMenu -> subMenu.getParentId().equals(menu.getId()))
                .map(subMenu -> covertMenuNode(subMenu, menuList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }

    /**
     * 修改菜单层级
     */
    private void updateLevel(UmsMenu umsMenu) {
        if (umsMenu.getParentId() == 0) {
            //没有父菜单时为一级菜单
            umsMenu.setLevel(0);
        } else {
            //有父菜单时选择根据父菜单level设置
            UmsMenu parentMenu = umsMenuMapper.selectById(umsMenu.getParentId());
            if (parentMenu != null) {
                umsMenu.setLevel(parentMenu.getLevel() + 1);
            } else {
                umsMenu.setLevel(0);
            }
        }
    }
}
